package com.ajaxjs.simpleApp.controller;

import java.io.IOException;
import java.util.Map;

import javax.mvc.annotation.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;

import com.ajaxjs.mvc.ModelAndView;
import com.ajaxjs.simpleApp.service.NewsService;
import com.ajaxjs.ioc.Bean;
import com.ajaxjs.ioc.Resource;
import com.ajaxjs.web.CommonController;
import com.ajaxjs.web.CommonEntryAdminController;
import com.ajaxjs.web.upload.UploadConfigByConfigFile;
import com.ajaxjs.web.upload.UploadRequest;
import com.ajaxjs.web.upload.UploadResult;

@Controller
@Path("/admin/news")
@Bean("NewsAdminController")
public class NewsAdminController extends CommonController<Map<String, Object>, Long, NewsService> implements CommonEntryAdminController<Map<String, Object>, Long> {
	@Resource("NewsService")
	private NewsService service;

	@GET
	@Path("list")
	@Override
	public String list(@QueryParam("start") int start, @QueryParam("limit") int limit, ModelAndView model) {
		initDb();
		model.put("catalogMenu", getService().getCatalog());
		super.pageList(start, limit, model);
		return String.format(jsp_adminList, getService().getTableName());
	}

	@GET
	@Override
	public String createUI(ModelAndView model) {
		return super.createUI(model);
	}

	@GET
	@Path("{id}")
	@Override
	public String editUI(@PathParam("id") Long id, ModelAndView model) {
		info(id, model);
		return String.format(jsp_adminInfo, getService().getTableName());
	}

	@POST
	@Override
	public String create(Map<String, Object> entity, ModelAndView model) {
		return super.create(entity, model);
	}

	@PUT
	@Path("{id}")
	@Override
	public String update(Map<String, Object> entity, ModelAndView model) {
		return super.update(entity, model);
	}

	@DELETE
	@Path("{id}")
	@Override
	public String delete(Long id, ModelAndView model) {
		return super.delete(id, model);
	}

	@GET
	@Path("catalog")
	public String newsCatalogUI() {
		return common_jsp_perfix + "simple_admin/edit-cataory";
	}

	@Override
	public void prepareData(ModelAndView model) {
		super.prepareData(model);
		initDb();
		model.put("catalogMenu", getService().getCatalog());

	}

	@POST
	@Path("imgUpload")
	public String imgUpload(HttpServletRequest request) throws IOException {
		UploadRequest u = new UploadRequest(request);
		u.setConfig(new UploadConfigByConfigFile());
		UploadResult result = u.upload();
		if (result.isOk)
			return String.format(json_ok, result.fileName + "上传成功！");
		else
			return String.format(json_not_ok, result.fileName + "上传失败！");
	}

	//	@GET
	//	@Path("catalog/list")
	//	public String getNewsCatalog(ModelAndView model, HttpServletRequest request) {
	//		initDb();
	//		prepareData(model);
	//		CatalogDao dao = new DaoHandler<CatalogDao>().bind(CatalogDao.class);
	//		List<Map<String, Object>> result = dao.findAll(new QueryParams(request.getParameterMap()));
	//
	//		return outputListBeanAsJson(result);
	//	}
}
